<?php

class CpdeficienciesController extends Core_Controller_Action 
{
	private $_api;
	
	public function init() {	
		$this->_api = new Deficiencies();
	}
	public function indexAction(){
		$this->_redirect('cpdeficiencies/list/type/0');
	}
	public function listAction(){
		$type = intval( $this->_request->getParam( 'type', 0 ) );
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		
		$pagerLayout = $this->_api->getListWithPager( $page, $this->view->baseUrl() , $type ? 'preorder' : 'normal');
		
		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;
	}
	public function disAction(){
		$prod = intval( $this->_request->getParam( 'prod', 0 ) );
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		
		$pagerLayout = $this->_api->getListWithPagerProd( $page, $this->view->baseUrl() , $prod);
		
		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;
		if($this->_request->isPost())
		{
			$post = $this->_request->getPost();
			foreach ($post['preorder'] as $key => $val)
			{
				if($val==1)
				{
					$api = new Deficiencies();
					$one = $api->getItem( $key )->getFirst();
					if(!$one->send())
					{
						$this->view->error = 'Zabrakło kluczy, wysyłka zostałą wstrzymana.';
						break;
					}
				}
			}
		}
	}
}